/**
 * create by zhanghao 2015年9月10日19:45:03
 */
$(document).ready(
		function() {
			$.ajax({
				url : "/NightFair/Seller/GetComment",
				data : {"action": "getnum", "pageNum":6,"pageNow":1},
				type : "post",
				success : function(data) {
				    var dataObj = JSON.parse(data);
					if (dataObj.status == 200) {
						$('#total_comment').append("您好，您共收到"+dataObj.commentsnum +"条评论,以下是评论记录:")
						dataObj.data.forEach(function(e){
				            $(".comment_total").append("<div class='dl_comment'> "+ 				            		
				            		"<dl class='user'>买　　家：<span class='grade'> "+e.nickname+"</span></dl>" +
				            		"<dl class='title-comment'>评　　分：<span class='grade-comment'> "+e.grade+"</span><span class='date-comment'> "+e.time+"</span></dl>" +	
				            		"<dl class='content-comment'>评价内容：<span class='comment'>"+e.comment+"</span></dl>"+				            					            		
				            		" </div>");
				        });
						$(".tcdPageCode").createPage({
					        pageCount:dataObj.pagenum,
					        current:1,
					        backFn:function(p){					        	
					            console.log(p);	
					            $.ajax({
									url : "/NightFair/Seller/GetComment",
									data : {"action": "select", "pageNum":6,"pageNow":p},
									type : "post",
									success : function(data) {
									    var dataObj = JSON.parse(data);
										if (dataObj.status == 200) {
											$(".comment_total").empty();
											dataObj.data.forEach(function(e){
												 $(".comment_total").append("<div class='dl_comment'> "+ 				            		
										            		"<dl class='user'>买　　家：<span class='grade'> "+e.nickname+"</span></dl>" +
										            		"<dl class='title-comment'>评　　分：<span class='grade-comment'> "+e.grade+"</span><span class='date-comment'> "+e.time+"</span></dl>" +	
										            		"<dl class='content-comment'>评价内容：<span class='comment'>"+e.comment+"</span></dl>"+				            					            		
										            		" </div>");
									        });
										} else {
											console.log("没有信息！");
										}
									},
									error : function(error) {
										console.log(error);
										alert('网络异常，稍后再试!');
									}
								});
					            
					        }
					    });
					} else {
						console.log("没有信息！");
					}
				},
				error : function(error) {
					console.log(error);
					alert('网络异常，稍后再试!');
				}
			});
		/*	$(".tcdPageCode").createPage({
		        pageCount:num,
		        current:1,
		        backFn:function(p){					        	
		            console.log(p);	
		            
		        }
		    });*/
		});
function logout()
{
    $.ajax({
        url: "/NightFair/Register",
        data: {"validate":"logout"},
        type: "post",
        success: function (data) {
            var dataObj = JSON.parse(data);
            if (dataObj.status ==200) {
                   location.replace("/NightFair/login.html");
            } else {
                alert("错误代码："+dataObj.status);
            }
        }, error: function (error) {
            console.log(error);
            alert('网络异常!');
        }
    });
}
(function($){
	var ms = {
		init:function(obj,args){
			return (function(){
				ms.fillHtml(obj,args);
				ms.bindEvent(obj,args);
			})();
		},
		//填充html
		fillHtml:function(obj,args){
			return (function(){
				obj.empty();
				//上一页
				if(args.current > 1){
					obj.append('<a href="javascript:;" class="prevPage">上一页</a>');
				}else{
					obj.remove('.prevPage');
					obj.append('<span class="disabled">上一页</span>');
				}
				//中间页码
				if(args.current != 1 && args.current >= 4 && args.pageCount != 4){
					obj.append('<a href="javascript:;" class="tcdNumber">'+1+'</a>');
				}
				if(args.current-2 > 2 && args.current <= args.pageCount && args.pageCount > 5){
					obj.append('<span>...</span>');
				}
				var start = args.current -2,end = args.current+2;
				if((start > 1 && args.current < 4)||args.current == 1){
					end++;
				}
				if(args.current > args.pageCount-4 && args.current >= args.pageCount){
					start--;
				}
				for (;start <= end; start++) {
					if(start <= args.pageCount && start >= 1){
						if(start != args.current){
							obj.append('<a href="javascript:;" class="tcdNumber">'+ start +'</a>');
						}else{
							obj.append('<span class="current">'+ start +'</span>');
						}
					}
				}
				if(args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 5){
					obj.append('<span>...</span>');
				}
				if(args.current != args.pageCount && args.current < args.pageCount -2  && args.pageCount != 4){
					obj.append('<a href="javascript:;" class="tcdNumber">'+args.pageCount+'</a>');
				}
				//下一页
				if(args.current < args.pageCount){
					obj.append('<a href="javascript:;" class="nextPage">下一页</a>');
				}else{
					obj.remove('.nextPage');
					obj.append('<span class="disabled">下一页</span>');
				}
			})();
		},
		//绑定事件
		bindEvent:function(obj,args){
			return (function(){
				obj.on("click","a.tcdNumber",function(){
					var current = parseInt($(this).text());
					ms.fillHtml(obj,{"current":current,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(current);
						
					}
				});
				//上一页
				obj.on("click","a.prevPage",function(){
					var current = parseInt(obj.children("span.current").text());
					ms.fillHtml(obj,{"current":current-1,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(current-1);
					}
				});
				//下一页
				obj.on("click","a.nextPage",function(){
					var current = parseInt(obj.children("span.current").text());
					ms.fillHtml(obj,{"current":current+1,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(current+1);
					}
				});
			})();
		}
	}
	$.fn.createPage = function(options){
		var args = $.extend({
			pageCount : 10,
			current : 1,
			backFn : function(){}
		},options);
		ms.init(this,args);
	}
	
})(jQuery);
